package runInfo;

import java.io.IOException;
import java.text.DecimalFormat;

import org.eclipse.swt.widgets.Display;
import com.phidgets.PhidgetException;
import com.phidgets.TemperatureSensorPhidget;


public class TestThreadTemp implements Runnable {

    // TODO: estimate rate we are pulling off at

    static double lastWrittenTemp = 1.1;
    static String lastWrittenBoilTemp = "1.1";
    static String lastWrittenBottleNum = "0";
    static String lastWrittenAbv = "0.0";

    static final Double minTempChangeForWrite = 0.25;

    //	@Override
    public void run() {
        long startTime = System.currentTimeMillis();
        long currentTime;
        TemperatureSensorPhidget tempsensor;
        try {
            tempsensor = new TemperatureSensorPhidget();
            tempsensor.openAny();
            System.out.println("waiting for TemperatureSensor attachment...");
            tempsensor.waitForAttachment();
            tempsensor.setTemperatureChangeTrigger(0, 0.2);
            tempsensor.setThermocoupleType(0, TemperatureSensorPhidget.PHIDGET_TEMPERATURE_SENSOR_J_TYPE);
            System.out.println("captured TemperatureSensor attachment.");
            while (Data.programRunning) {
                currentTime = System.currentTimeMillis();
                Data.timeS = String.valueOf(((currentTime - startTime) / 1000.0));
                GetTemps(tempsensor, startTime);
                try {
                    Thread.sleep((long) 1000);            // sleep for X ms
                } catch (InterruptedException e) {    int unableToSleep = 1;
                }
            }
        } catch (PhidgetException e2) {
            e2.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("No longer measureing temperature ------------------------------------");
    }


    public void GetTemps(TemperatureSensorPhidget tempsensor, Long startTime) throws PhidgetException, IOException {
        double calibration = 0.3;
        Data.tempTC = tempsensor.getTemperature(0) - calibration;
        Data.tempAC = tempsensor.getAmbientTemperature() - calibration;
        Data.tempTF = Data.tempTC * 9 / 5 + 32;
        Data.tempAF = Data.tempAC * 9 / 5 + 32;

        DecimalFormat df = new DecimalFormat("###.###");
        Data.tempTC = Double.valueOf(df.format(Data.tempTC));
        Data.tempAC = Double.valueOf(df.format(Data.tempAC));
        Data.tempTF = Double.valueOf(df.format(Data.tempTF));
        Data.tempAF = Double.valueOf(df.format(Data.tempAF));

        double runningTime = (1.0 / 1000.0) * (1.0 / 60.0) * (System.currentTimeMillis() - startTime);
        Data.milliTime = System.currentTimeMillis();

        Data.timeS = String.valueOf(runningTime);

        double tempChange = Data.tempTC - lastWrittenTemp;
        if (Math.abs(tempChange) >= minTempChangeForWrite ||
                lastWrittenBoilTemp.contentEquals(Data.boilTempS) ||               // != Data.boilTempS ||
                lastWrittenBottleNum.contentEquals(Data.bottleS) ||                // != Data.bottleS ||
                lastWrittenAbv.contentEquals(Data.corrAbvS)                        // != Data.corrAbvS
                ) {
            lastWrittenTemp = Data.tempTC;
            lastWrittenBoilTemp = Data.boilTempS;
            lastWrittenBottleNum = Data.bottleS;
            lastWrittenAbv = Data.corrAbvS;
            Writer.WriteData(runningTime);
        }

        Display.getDefault().asyncExec(new Runnable() {
            public void run() {
                TestThreadGUI.tempTextC.setText(String.valueOf(Data.tempTC));
                TestThreadGUI.tempTextF.setText(String.valueOf(Data.tempTF));
                TestThreadGUI.temptextAC.setText(String.valueOf(Data.tempAC));
                TestThreadGUI.temptextAF.setText(String.valueOf(Data.tempAF));
                Data.corrAbvS = TestThreadGUI.corrAbvText.getText();
                Data.measAbvS = TestThreadGUI.measAbvText.getText();
                Data.boilTempS = TestThreadGUI.boilerTempCombo.getText();
                Data.distTemp = TestThreadGUI.distTempCombo.getText();
            }
        });
    }


}
